package com.ownku;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ProxyIP {
	static int i=0;
	static int count=-1;
	static ArrayList<IpPort> arrayList = null;
	public static IpPort getIp() {
		
		if (count==-1) {
			arrayList=getIpPortList();
			count=arrayList.size();
		}
		IpPort ipPort=arrayList.get(i%count);
		
		
		i++;
		
		return ipPort;
		
	}
	public static HashMap<String, String> getIpPort() {
		
		HashMap<String, String> hashMap=new HashMap<String, String>();
		String path="1.txt";
		File file=new File(path);
		try {
			BufferedReader fileReader=new BufferedReader(new InputStreamReader(new FileInputStream(file)));
		    String line=null;
		    while ((line=fileReader.readLine())!=null) {
		    	
		    	if (line.equals("")||line.startsWith("#")) {
					continue;
				}
		    	
		    	line=removeSpecilChar(line);
				String[] strings =line.split(":");
				hashMap.put(strings[0], strings[1]);
		    	
		    	
			}
		
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			
		}
		
		return hashMap;

	}
	
public static ArrayList<IpPort> getIpPortList() {
		
		ArrayList<IpPort> arrayList=new ArrayList<IpPort>();
		String path="1.txt";
		File file=new File(path);
		try {
			BufferedReader fileReader=new BufferedReader(new InputStreamReader(new FileInputStream(file)));
		    String line=null;
		    IpPort ipPort=null;
		    while ((line=fileReader.readLine())!=null) {
		    	
		    	if (line.equals("")||line.startsWith("#")) {
					continue;
				}
		    	
		    	line=removeSpecilChar(line);
				String[] strings =line.split(":");
//				hashMap.put(strings[0], strings[1]);
		    	ipPort=new IpPort();
		    	ipPort.setIp(strings[0]);
		    	ipPort.setPort(strings[1]);
		    	arrayList.add(ipPort);
		    	
			}
		
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			
		}
		
		return arrayList;

	}
	public static String removeSpecilChar(String str){
		String result = "";
		if(null != str){
		Pattern pat = Pattern.compile("\\s*|\n|\r|\t");
		Matcher mat = pat.matcher(str);
		result = mat.replaceAll("");
		}
		return result;
	}
	
    public static void main(String[] args) {
    	
    	for (int i = 0; i < 40; i++) {
    		System.out.println(getIp());
		}
    }
	
	  
}

